//
//  Assert Attributes of Current Webpage.m
//  Assert Attributes of Current Webpage
//
//  Created by itod on 3/30/07.
//  Copyright 2007 Todd Ditchendorf. All rights reserved.
//

#import "Assert Attributes of Current Webpage.h"
#import <OSAKit/OSAKit.h>


@interface Assert_Attributes_of_Current_Webpage (Private)
- (NSString *)pathForJavaScriptResource:(NSString *)filename;
- (NSString *)assertionFailedDescription;
@end

@implementation Assert_Attributes_of_Current_Webpage

	
- (NSAppleEventDescriptor *)executeAndReturnError:(NSDictionary **)errorInfo
{
	NSAppleEventDescriptor *desc = [super executeAndReturnError:errorInfo];
	
	NSString *retVal = [desc stringValue];
	BOOL assertionPassed = [[retVal lowercaseString] hasPrefix:@"true"];
	//NSLog(@"assertionPassed : %d", assertionPassed);
	
	if (!assertionPassed) {
		NSMutableDictionary *d = nil;
		if (*errorInfo) {
			d = [NSMutableDictionary dictionaryWithDictionary:(*errorInfo)];
		} else {
			d = [NSMutableDictionary dictionary];
		}
		
		NSString *errStr = [self assertionFailedDescription];
		
		int i = [[desc stringValue] rangeOfString:@":"].location + 1;
		NSString *urlStr = [[desc stringValue] substringFromIndex:i];
		
		[d setObject:[NSNumber numberWithInt:47] forKey:OSAScriptErrorNumber];
		[d setObject:errStr forKey:OSAScriptErrorMessage];
		[d setObject:errStr forKey:@"OttoMateAssertionFailure"];
		[d setObject:urlStr forKey:@"OttoMateURL"];
		(*errorInfo) = d;
		
		NSLog([(*errorInfo) description]);

		[super runAssertionFailedScript:(*errorInfo)];
	}
	
	return desc;
}

		
- (NSString *)javaScriptSource
{
	NSString *string1 = [self objectForParameterKey:@"string1"];
	string1 = (string1) ? string1 : @"";
	NSString *string2 = [self objectForParameterKey:@"string2"];
	string2 = (string2) ? string2 : @"";
	NSString *formIdentifier = [self objectForParameterKey:@"formIdentifier"];

	NSString *filename = nil;
	NSMutableString *jsSource = nil;
	
	int assertionType = [[self objectForParameterKey:@"assertionType"] intValue];
	
	switch (assertionType) {

		case TODAssertionTypePageTitleEquals:
			filename = @"AssertPageTitleEquals";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypePageHasElementWithId:
			filename = @"AssertPageHasElementWithId";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypePageDoesntHaveElementWithId:
			filename = @"AssertPageDoesntHaveElementWithId";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypePageContainsText:
			filename = @"AssertPageContainsText";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypePageDoesntContainText:
			filename = @"AssertPageDoesntContainText";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypePageContainsHTML:
			filename = @"AssertPagePageContainsHTML";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypePageDoesntContainHTML:
			filename = @"AssertPageDoesntContainHTML";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1];
			break;
			
		case TODAssertionTypeValueForFormElementWithId:
			filename = @"AssertValueForFormElementWithId";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, string1, string2];
			break;
			
		case TODAssertionTypeValueForFormElementWithName:
			filename = @"AssertValueForFormElementWithName";
			jsSource = [NSString stringWithContentsOfFile:[self pathForJavaScriptResource:filename]];	
			jsSource = [NSString stringWithFormat:jsSource, formIdentifier, string1, string2];
			break;

	}
	
	jsSource = [NSMutableString stringWithString:jsSource];
	[jsSource replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:nil range:NSMakeRange(0, [jsSource length])];

	//NSLog(@"jsSource: %@", jsSource);

	return jsSource;
}


- (NSString *)pathForJavaScriptResource:(NSString *)filename
{	
	NSBundle *bundle = [NSBundle bundleForClass:[self class]];
	return [bundle pathForResource:filename ofType:@"js" inDirectory:@"JavaScript"];
}


- (NSString *)assertionFailedDescription
{
	NSString *string1 = [self objectForParameterKey:@"string1"];
	NSString *string2 = [self objectForParameterKey:@"string2"];
	NSString *formIdentifier = [self objectForParameterKey:@"formIdentifier"];

	NSString *fmt = nil;
	NSString *result = nil;

	int assertionType = [[self objectForParameterKey:@"assertionType"] intValue];
	
	switch (assertionType) {
		
		case TODAssertionTypePageTitleEquals:
			fmt = @"AssertPageTitleEquals failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypePageHasElementWithId:
			fmt = @"AssertPageHasElementWithId failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypePageDoesntHaveElementWithId:
			fmt = @"AssertPageDoesntHaveElementWithId failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypePageContainsText:
			fmt = @"AssertPageContainsText failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypePageDoesntContainText:
			fmt = @"AssertPageDoesntContainText failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypePageContainsHTML:
			fmt = @"AssertPagePageContainsHTML failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypePageDoesntContainHTML:
			fmt = @"AssertPageDoesntContainHTML failed for '%@'";
			result = [NSString stringWithFormat:fmt, string1];
			break;
			
		case TODAssertionTypeValueForFormElementWithId:
			fmt = @"AssertValueForFormElementWithId failed for id '%@', value '%@'";
			result = [NSString stringWithFormat:fmt, string1, string2];
			break;
			
		case TODAssertionTypeValueForFormElementWithName:
			fmt = @"AssertValueForFormElementWithName failed for form name '%@', element name '%@', value '%@'";
			result = [NSString stringWithFormat:fmt, formIdentifier, string1, string2];
			break;

	}

	return result;
}

@end
